package net.homeip.jcperera.nu.core.msg;

import net.homeip.jcperera.nu.core.CoreBaseInterface;
import net.homeip.jcperera.nu.core.utils.ReflectionUtils;

/**
 * Provides utility functions, this is not a proper framework class, just a function library.
 * 
 * @name LoggingUtils functions
 * @author julio.c.perera@gmail.com
 * @application nu
 * @since 0.1.0.0
 */
public class LoggingUtils
{
    public static String getCoreBaseInfoAsString(int levelsUp)
    {
        String result = "";
        String callingClassName = null;

        StackTraceElement ste = ReflectionUtils.getCallerStackTraceElement(levelsUp + 1);
        if (ste != null) callingClassName = ste.getClassName();
        Class<?> callerClass = ReflectionUtils.getClassFromClassName(callingClassName);
        if (callerClass != null && ReflectionUtils.isInterfaceImplementedInClass(CoreBaseInterface.class, callerClass))
        {
            try
            {
                result = "NU >>> RuntimeLevel:[" + callerClass.getField("runtimeLevel").getInt(null) + "] >> Module:["
                        + callerClass.getField("moduleName").get(null) + "] >> Component:["
                        + callerClass.getField("componentName").get(null) + "] >> Method:[" + callingClassName + "."
                        + ste.getMethodName() + "()] >> Message: ";
            }
            catch (NoSuchFieldException e)
            {
                result = "";
            }
            catch (IllegalAccessException e)
            {
                result = "";
            }
        }
        return result;
    }
}
